<template>
  <div class="tag-management-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>档案标签管理</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="addTag">添加标签</el-button>
      </div>
      <div class="search-bar">
        <el-input placeholder="请输入标签名称或描述" v-model="searchQuery" class="input-with-select" @keyup.enter.native="fetchTags">
          <el-button slot="append" icon="el-icon-search" @click="fetchTags"></el-button>
        </el-input>
      </div>
      <el-table :data="tags" style="width: 100%">
        <el-table-column prop="tagId" label="ID" width="80"></el-table-column>
        <el-table-column prop="tagName" label="标签名称"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column prop="createdAt" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- Dialog for Add/Edit Tag -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
      <el-form :model="currentTag" label-width="100px">
        <el-form-item label="标签名称" prop="tagName" :rules="[{ required: true, message: '请输入标签名称', trigger: 'blur' }]">
          <el-input v-model="currentTag.tagName"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="currentTag.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveTag">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import service from '@/utils/request';

export default {
  name: 'TagManagement',
  data() {
    return {
      tags: [],
      dialogVisible: false,
      dialogTitle: '',
      currentTag: {
        tagId: null,
        tagName: '',
        description: ''
      },
      searchQuery: ''
    };
  },
  created() {
    this.fetchTags();
  },
  methods: {
    fetchTags() {
      service.get('/tag/list', {
        params: {
          search: this.searchQuery
        }
      })
        .then(res => {
          if (res.data.code === 200) {
            this.tags = res.data.data;
          } else {
            this.$message.error(res.data.message || '获取标签列表失败');
          }
        })
        .catch(error => {
          console.error("Error fetching tags:", error);
          this.$message.error('获取标签列表失败');
        });
    },
    addTag() {
      this.dialogTitle = '添加标签';
      this.currentTag = {
        tagId: null,
        tagName: '',
        description: ''
      };
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.dialogTitle = '编辑标签';
      this.currentTag = { ...row };
      this.dialogVisible = true;
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        service.delete(`/tag/delete/${row.tagId}`)
          .then(res => {
            if (res.data.code === 200) {
              this.$message.success('删除成功');
              this.fetchTags();
            } else {
              this.$message.error(res.data.message || '删除失败');
            }
          })
          .catch(error => {
            console.error("Delete failed:", error);
            this.$message.error('删除失败');
          });
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    saveTag() {
      if (this.currentTag.tagId) {
        service.put('/tag/update', this.currentTag)
          .then(res => {
            if (res.data.code === 200) {
              this.$message.success('更新成功');
              this.dialogVisible = false;
              this.fetchTags();
            } else {
              this.$message.error(res.data.message || '更新失败');
            }
          })
          .catch(error => {
            console.error("Update failed:", error);
            this.$message.error('更新失败');
          });
      } else {
        service.post('/tag/add', this.currentTag)
          .then(res => {
            if (res.data.code === 200) {
              this.$message.success('添加成功');
              this.dialogVisible = false;
              this.fetchTags();
            } else {
              this.$message.error(res.data.message || '添加失败');
            }
          })
          .catch(error => {
            console.error("Add failed:", error);
            this.$message.error('添加失败');
          });
      }
    }
  }
}
</script>

<style scoped>
.tag-management-container {
  padding: 20px;
}
.search-bar {
  margin-bottom: 20px;
  width: 50%; /* Adjust width as needed */
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}
.box-card {
  width: 100%;
}
</style> 